/**************************************************************************************/
function Spreadsheet()
{
	this.init();
}

Spreadsheet.prototype.init=function() {
	this.name="";
	this.worksheets=[];
}

Spreadsheet.prototype.load=function() {

}

Spreadsheet.prototype.save=function() {

}

Spreadsheet.prototype.worksheet=function(i) {
	return this.worksheets[i];
}

Spreadsheet.prototype.worksheet_add=function() {
	var w=new SpreadsheetWorksheet();
	this.worksheets.push(w);
	return w;
}

/**************************************************************************************/
function SpreadsheetWorksheet()
{
	this.name="";
	this.cols=0;
	this.rows=0;
	this.cells=[];
}

SpreadsheetWorksheet.prototype.grow=function(row,col)
{
//	system.stderr.write("SpreadsheetWorksheet.prototype.grow("+row+","+col+")\n");system.stderr.flush();
	if (this.cols<=col) {
		for (var i=0;i<this.rows;i++)
			for (var j=this.cols;j<=col;j++)
				this.cells[i].push(new SpreadsheetCell());
		this.cols=col+1;
	}
	if (this.rows<=row) {
		for (var i=this.rows;i<=row;i++) {
			var r=[];
			this.cells.push(r);
			for (var j=0;j<this.cols;j++) r.push(new SpreadsheetCell());
		}
		this.rows=row+1;
	}
}

SpreadsheetWorksheet.prototype.read=function(row,col)
{
	this.grow(row,col);
	return this.cells[row][col].get_data();
}

SpreadsheetWorksheet.prototype.write=function(row,col,data,format)
{
	this.grow(row,col);
	this.cells[row][col].write(data,format);
}

SpreadsheetWorksheet.prototype.csv_load=function(filename,options)
{
	var f=new File(filename);
	f.open(r);
	var text=f.read().toString("utf-8");
	f.close();
	this.csv_parse(text,options);
}

SpreadsheetWorksheet.prototype.csv_parse=function(text,options)
{
	if (text.substr(text.length-1)!="\n" && text.substr(text.length-1)!="\r") text=text+"\n";
//	system.stderr.write("SpreadsheetWorksheet.prototype.parse()\n");system.stderr.flush();
	options=this.csv_options_fill(options);
	var row=0;
	var col=0;
	var flag=true;
	var cnt=0;
	while (flag) {
//		system.stderr.write("	SpreadsheetWorksheet.prototype.parse() iteration, text.length="+text.length+"\n");system.stderr.flush();
		var a;
		if (a=text.match(options.cell_re)) {
//			system.stderr.write("	SpreadsheetWorksheet.prototype.parse() iteration a[1]="+a[1]+", a[2]="+a[2]+"\n");system.stderr.flush();
			this.write(row,col,options.cell_unformat(a[1]));
			if (a[2].match(options.newline_re)) {
				row++;
				col=0;
			} else {
				col++;
			}
			var l=a[1].length+a[2].length;
			var size=text.length;
			text=text.substr(l);
			if (!l) throw "cell_re() match strange error, text='"+text.substr(0,500)+"...', a[1]="+a[1]+", a[2]="+a[2];
			if (size<=text.length) throw "Size error, was "+size+", now "+text.length+"\n, leftover text is:\n"+text;
			cnt++;
			if (cnt==1000) throw "TEMP";
		} else {
//			system.stderr.write("	SpreadsheetWorksheet.prototype.parse() iteration error\n");system.stderr.flush();
			flag=false;
		}
	}
	return text;
//	text.split(options.newline_re).forEach(function(line) {
//		
//	});
}

SpreadsheetWorksheet.prototype.html_table_gen=function()
{
	r="<table class='spreadsheet-wb'>\n";
	for (var i=0;i<this.rows;i++) {
		r+="<tr>";
		for (var j=0;j<this.cols;j++) {
			r+="<td>";
			r+=this.cells[i][j].get_data();
			r+="</td>";
		}
		r+="</tr>\n";
	}

	r+="</table>\n";
	return r;
}

SpreadsheetWorksheet.prototype.csv_save=function(filename,options)
{
	var csv=this.csv_gen(options);
	var f=new File(filename);
	f.open("w");
	f.write(csv);
	f.close();
}

SpreadsheetWorksheet.prototype.csv_options_fill=function(options)
{
	if (!options) options={};
	if (!options.newline) options.newline="\r\n";
	if (!options.newline_re) options.newline_re=new RegExp(/(\r|\n|\r\n)/);
	if (!options.separator) options.separator=",";
	if (!options.cell_re) options.cell_re=new RegExp(/^(|[^",\r\n]+|"(?:[^"]+|\r|\n|"")*")([\r\n]+|,)/);
	if (!options.simpletext) options.simpletext=new RegExp(/^[a-zA-Z0-9\.]*$/);
	if (!options.cell_start) options.cell_start='"';
	if (!options.cell_end) options.cell_end='"';
	if (!options.cell_format) options.cell_format=function(text) {
		return text.replace(/\\/g,'\\').replace(/"/g,'""');//.replace(/\n/g,"\\n").replace(/\r/g,"\\r");
		//return text.replace(/\\/g,'\\\\').replace(/"/g,'\\"').replace(/\n/g,"\\n").replace(/\r/g,"\\r");
	};
	if (!options.cell_unformat) options.cell_unformat=function(text) {
		return text.replace(/^"/,"").replace(/"$/,"").replace(/""/g,'"');
	};
	return options;
}

SpreadsheetWorksheet.prototype.csv_gen=function(options)
{
	options=this.csv_options_fill(options);
	var r="";
	for (var i=0;i<this.rows;i++) {
		for (var j=0;j<this.cols;j++) {
			if (j) r+=options.separator;
			r+=this.csv_gen_cell(options,i,j);
		}
		r+=options.newline;
	}
	return r;
}

SpreadsheetWorksheet.prototype.csv_gen_cell=function(options,i,j)
{
	var text=this.cells[i][j].get_data();
	if (options.simpletext.test(text)) return text;
	return options.cell_start+options.cell_format(text)+options.cell_end;
}

/**************************************************************************************/
function SpreadsheetCell()
{
	this.data="";
	this.format=undefined;
}

SpreadsheetCell.prototype.write=function(data,format)
{
	this.data=data;
	this.format=format;
}

SpreadsheetCell.prototype.get_data=function()
{
	return this.data;
}

/**************************************************************************************/
exports.Spreadsheet=Spreadsheet;

